import 'package:flutter/material.dart';
import 'package:flutter_smart_heating/common/http.dart';
import 'package:flutter_smart_heating/ui/ui.dart';

class UserProfilePage extends StatefulWidget {
  const UserProfilePage({super.key});

  @override
  State<UserProfilePage> createState() => _UserProfilePageState();
}

class _UserProfilePageState extends State<UserProfilePage> {
  Map<String, dynamic> user = {};
  List<dynamic> log = [];
  fetchData() async {
    var res = await Http.instance.post('/api/information/user/profile');
    setState(() {
      user = res.data;
    });
  }

  fetchLog() async {
    var res = await Http.instance.post('/api/information/user/log');
    setState(() {
      log = res.data;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('个人信息'),
      ),
      body: UiLifeCycle(
        onRouteDone: () {
          fetchData();
          fetchLog();
        },
        child: SingleChildScrollView(
          padding: const EdgeInsets.symmetric(horizontal: UiTheme.spacingX),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              titleView('帐号信息'),
              UiPanel(
                children: [
                  cell(title: "用户ID", value: user['user_id']),
                  cell(title: "用户名", value: user['user_login']),
                  cell(
                    title: "手机号",
                    value: user['mobile'],
                    showDivider: false,
                  ),
                ],
              ),
              const UiLineBreak(),
              titleView('管辖范围'),
              UiPanel(
                children: [
                  cell(
                    title: "管辖地区",
                    value: user['substation_name'],
                    showDivider: false,
                  ),
                ],
              ),
              const UiLineBreak(),
              titleView('最后登陆'),
              UiPanel(
                children: [
                  cell(title: "最后登录时间", value: user['last_login_time']),
                  cell(
                    title: "最后登录地点",
                    value: user['last_login_ip'],
                    showDivider: false,
                  ),
                ],
              ),
              const UiLineBreak(),
              if (log.isNotEmpty) titleView('操作日志'),
              if (log.isNotEmpty)
                UiPanel(
                  children: List.generate(
                    log.length,
                    (index) {
                      var item = log[index];
                      return Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            padding: const EdgeInsets.symmetric(
                                horizontal: 15, vertical: 12),
                            child: Row(
                              children: [
                                Expanded(
                                  child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      Text(
                                        item['title'] ?? '--',
                                        style: const TextStyle(
                                          fontSize: 16,
                                        ),
                                      ),
                                      Row(
                                        children: [
                                          const Icon(
                                            UiIcons.location_fill,
                                            size: 13,
                                            color: UiTheme.primary,
                                          ),
                                          Text(item['ip']),
                                        ],
                                      ),
                                    ],
                                  ),
                                ),
                                Text(
                                  item['create_time'],
                                )
                              ],
                            ),
                          ),
                          if (index < log.length - 1)
                            const Divider(
                              height: 0,
                              indent: 15,
                            ),
                        ],
                      );
                    },
                  ),
                ),
              const UiLineBreak(),
            ],
          ),
        ),
      ),
    );
  }

  Widget titleView(title) {
    return Padding(
      padding: const EdgeInsets.only(left: 16, bottom: 5, top: 5),
      child: Text(
        title,
        style: const TextStyle(color: Colors.grey),
      ),
    );
  }

  Widget cell({
    String title = '',
    dynamic value,
    bool showDivider = true,
  }) {
    return UiListCell(
      backgroundColor: Colors.transparent,
      title: Text(title),
      dividerIndent: 15,
      showDivider: showDivider,
      trailing: Opacity(opacity: 0.8, child: Text('${value ?? '--'}')),
    );
  }
}
